package com.hospital.hospitalsystemgateway.config;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @author qiancheng@cqxyy.net
 * @date 2022/11/4
 */

/**
 * 拦截异常返回
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ResponseBody
    @ExceptionHandler(MyException.class)
    public Object handlerException(MyException e){
        String msg = e.getErrorMsg();
        if(msg == null || msg.equals("")){
            msg = "服务器出错";
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("message",msg);
        jsonObject.put("status",e.getErrorCode());
        e.printStackTrace();
        log.error(e.getMessage());
        return jsonObject;
    }
}
